package com.conghu.crm.workbench.service.impl;

import com.conghu.crm.settings.dao.UserDao;
import com.conghu.crm.settings.domain.User;
import com.conghu.crm.utils.DateTimeUtil;
import com.conghu.crm.utils.SqlSessionUtil;
import com.conghu.crm.utils.UUIDUtil;
import com.conghu.crm.vo.PageListVo;
import com.conghu.crm.workbench.dao.ActivityDao;
import com.conghu.crm.workbench.dao.ActivityRemarkDao;
import com.conghu.crm.workbench.domain.Activity;
import com.conghu.crm.workbench.domain.ActivityRemark;
import com.conghu.crm.workbench.service.ActivityService;
import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityServiceImpl implements ActivityService {
    private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    @Override
    public boolean addActivity(Activity activity) {

        int res = activityDao.insert(activity);

        return res != 0;
    }

    @Override
    public PageListVo<Activity> searchActivity(Map<String, String> map) {
        //JSON : {"pageNum":..., "pageSize":..., "name":..., "owner":..., "startDate":..., "endDate":...}
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        //PageHelper组件帮助分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> activityList = activityDao.search(map);
        //另一次查询用于查询符合条件的条数
        int total = activityDao.searchTotal(map);

        //返回一个VO对象
        PageListVo<Activity> activityPageListVo = new PageListVo<>();
        activityPageListVo.setTotal(total);
        activityPageListVo.setDataList(activityList);
        return activityPageListVo;
    }

    @Override
    public boolean deleteActivity(String[] ids) {
        //删除活动，要先删除与其关联的所有活动备注
        int count1 = activityRemarkDao.getCountByAids(ids);
        int count2 = activityRemarkDao.deleteByAid(ids);

        //再删除所有的活动
        int count3 = activityDao.delete(ids);

        return (count1 == count2) && (count3 == ids.length);
    }

    @Override
    public Map<String, Object> getUserListAndActivity(String id) {
        //查所有的用户列表
        List<User> userList = userDao.selectAll();
        //查id对应的活动信息
        Activity activity = activityDao.selectById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("uList", userList);
        map.put("a", activity);

        return map;
    }

    @Override
    public boolean update(Activity activity) {
        int res = activityDao.update(activity);
        return res == 1;
    }

    @Override
    public Activity detail(String id) {
        //查id对应的活动信息
        Activity activity = activityDao.selectDetail(id);
        return activity;
    }

    @Override
    public List<ActivityRemark> getRemarkListByAid(String activityId) {
        //查id对应的备注list
        return activityRemarkDao.selectByAid(activityId);
    }

    @Override
    public boolean saveRemark(ActivityRemark activityRemark) {
        //插入一个活动备注
        int res = activityRemarkDao.insert(activityRemark);
        return res != 0;
    }

    @Override
    public boolean updateRemark(Map<String, String> map) {
        //更新
        map.put("editFlag", "1");
        int res = activityRemarkDao.update(map);
        return res == 1;
    }

    @Override
    public boolean deleteRemark(String id) {
        //删除
        int res = activityRemarkDao.delete(id);
        return res == 1;
    }
}
